package com.cen.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cen.commonutils.R;
import com.cen.exceptionhandler.GuliException;
import com.cen.serviceedu.entity.EduTeacher;
import com.cen.serviceedu.entity.vo.TeacherQuery;
import com.cen.serviceedu.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author cen
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/edu-teacher")
public class EduTeacherController {
//    查询讲师表中所有数据
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "讲师用户列表")
    @GetMapping("/findAll")
    public R list(){
        List<EduTeacher> list = eduTeacherService.list(null);
        System.out.println(list);
        return R.ok().data("items",list);

    }
    @ApiOperation(value = "根据id删除教师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name="id",value = "讲师id",required = true)
            @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if (b){
        return R.ok();
        }
        else {
            return R.error();
        }
    }


    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
    @ApiParam(name = "page", value = "当前页码", required = true)
    @PathVariable Long page,
    @ApiParam(name = "limit", value = "每页记录数", required = true)
    @PathVariable Long limit){
       int i = 10/0;
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        eduTeacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }


//    条件查询带分页的方法
    @GetMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  TeacherQuery teacherQuery){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
//        构建条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
//        多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        try {
            int i = 10/0;
        } catch (Exception e) {
            throw new GuliException(20001,"自定义异常");
        }


//       调用方法
        eduTeacherService.page(pageTeacher,queryWrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    @PostMapping("addTeacher")
    public R addTeacher(EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);

    }
    @PatchMapping("/updateTeacher")
    public R updateTeacher(EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        return R.ok();
    }


}

